﻿using MediatR;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.Domains.ScratchTheFat;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.DTOs.ReportDTOs;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.SplitDetails
{
    public class SplitsSummaryHander : IRequestHandler<SplitsSummaryCommand, Resqults<SplitSummaryDTO>>
    {
        private readonly IBateRepository<SplitRU> splitRu;
        private readonly IBateRepository<Stash> stash;

        public SplitsSummaryHander(IBateRepository<SplitRU> splitRu, IBateRepository<Stash> stash)
        {
            this.splitRu = splitRu;
            this.stash = stash;
        }

        public async Task<Resqults<SplitSummaryDTO>> Handle(SplitsSummaryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults<SplitSummaryDTO> res = new Resqults<SplitSummaryDTO>();
                var list = (from a in splitRu.GetAll()
                           join b in stash.GetAll() on a.RepositoryID equals b.Stashid
                           group new { a, b } by new { a.RepositoryID, b.StashName } into c
                           select new SplitSummaryDTO()
                           {
                               RepositoryName=c.Key.StashName,
                               WeightNum=c.Sum(x=>x.a.WeightNum),
                               NetWeight=c.Sum(x=>x.a.NetWeight* x.a.WeightNum)
                           }).ToList();
                res.Date = list;
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
